/*************************************************
Author: will
Date: 9/22/2012
Description:
Notes: 
*************************************************/

#ifndef __CMapView_h__
#define __CMapView_h__

#include "CTArray.h"
#include "commclass.h"
#include "CTHashObjectMgr.h"

class MsgAnsMapInfo;

//地图上的用户
class CMapUser
{
public:
    typedef CTLib::CTArray<unsigned int, MAP_VIEW_COUNT_BECAUSE_CHAT_FRIEND> CHAT_VIEW_FRIENDS_GID;

	CMapUser() 
    {
	}

	virtual ~CMapUser() 
    {
	}

    int Clear()
    {
        _uiGID = 0;
        _bIsInOthersView = false;
        _astChatViewFriendsGID.Clear();

        return 0;
    }

public:
	unsigned int     _uiGID;
    bool             _bIsInOthersView;
    CHAT_VIEW_FRIENDS_GID  _astChatViewFriendsGID;  //如果我移动了，需要通知这些用户
};

//单个地图的用户视野
class CSingleMapView
{
public:
    typedef CTLib::CTArray<unsigned int, MAX_PLAYER_ONLINE> MAP_VIEW_NOTIFY_USERS_ARRAY;
    typedef CTLib::CTHashObjectMgr<unsigned int, CMapUser> MAP_VIEW_USER_HASH;

    CSingleMapView() 
        :_iUserCountInview(0)
    {
    }

    virtual ~CSingleMapView() 
    {
    }
 
    static size_t CountSize()
    {
        return MAP_VIEW_USER_HASH::CountSize(MAX_PLAYER_ONLINE);
    }

    int Init(const char* pBuffer, int iUserCountPerMap);
    int Clear();

    int IntoMap(unsigned int uiGID, bool bIsFamous, bool& bIsInOtherView);
    int LeaveMap(unsigned int _uiGID, CSingleMapView::MAP_VIEW_NOTIFY_USERS_ARRAY& astNotifyUsers);
    int Move(unsigned int uiGID, int iX, int iY, MAP_VIEW_NOTIFY_USERS_ARRAY& astNotifyUsers);
    int AddChatFriendView(unsigned int uiGID, unsigned int uiFriendGID);
    int GetMapUsers(unsigned int uiMapID, unsigned int uiGID, CSingleMapView::MAP_VIEW_NOTIFY_USERS_ARRAY& astMapUsers);
    int GetAllMapUsers(CSingleMapView::MAP_VIEW_NOTIFY_USERS_ARRAY& astMapUsers);

    bool IsRoleInMap(unsigned int uiGID);

    int Size()
    {
        return _stMapUsersHash.Size(MAP_VIEW_USER_HASH::USED_LIST);
    }

private:
    int _iUserCountInview;                                                //当前在用户视野中的用户数量
    CMapUser  _stMapUserTmp;                                                //主要用于在切换地图时保存ChatFriend
    MAP_VIEW_USER_HASH  _stMapUsersHash;                                    //地图上的用户
};

class CMapViews
{
public:
	CMapViews() 
    {
	}

	virtual ~CMapViews() 
    {
	}

    static size_t CountSize()
    {
        return CTLib::CTHashObjectMgr<unsigned short, int>::CountSize(MAP_VIEW_COUNT) 
                    + MAP_VIEW_COUNT * CSingleMapView::CountSize();
    }

    int Init(const char* pBuffer);

    int IntoMap(unsigned int uiMapID, unsigned int uiGID, bool bIsFamous, unsigned short usPosX = 0xFFFF, unsigned short usPosY = 0xFFFF);

    int LeaveMap(unsigned int uiMapID, unsigned int uiGID, CSingleMapView::MAP_VIEW_NOTIFY_USERS_ARRAY& astNotifyUsers);

    int Move(unsigned int uiMapID, unsigned int uiGID, int iX, int iY, 
                        CSingleMapView::MAP_VIEW_NOTIFY_USERS_ARRAY& astNotifyUsers);

    int GetMapUsers(unsigned int uiMapID, unsigned int uiGID, CSingleMapView::MAP_VIEW_NOTIFY_USERS_ARRAY& astMapUsers);
    int GetAllMapUsers(unsigned int uiMapID, CSingleMapView::MAP_VIEW_NOTIFY_USERS_ARRAY& astMapUsers);
    int GetAllMapUsersNum(unsigned int uiMapID);

    int AddChatFriendView(unsigned int uiMapID, unsigned int uiGID, unsigned int uiFriendGID);
    
    int NotifyMapUser(unsigned int uiMapID, unsigned int uiGID, unsigned short usPosX = 0xFFFF, unsigned short usPosY = 0xFFFF);
    int NotifyMapUser(unsigned int uiGID);

    bool IsRoleInMap(unsigned int uiMapID, unsigned int uiGID);

private:
    CTLib::CTArray<CSingleMapView, MAP_VIEW_COUNT>                  _astMapViews;
    CTLib::CTHashObjectMgr<unsigned int, int>                      _stMapViewIndexHash;
};


#endif // __CMapView_h__

